package com.leoman.plz_p.weidget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.leoman.plz_p.R;

import butterknife.Bind;
import butterknife.OnClick;
import cn.com.corelib.utils.DisplayUtil;
import cn.com.corelib.views.navigation.TranslucentNavBar;

public class NavBar extends TranslucentNavBar {


    @Bind(R.id.ll_back)
    LinearLayout ivBack;
    @Bind(R.id.tv_left_text)
    TextView tvLeftText;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right_text)
    TextView tvRightText;
    @Bind(R.id.tv_to_right_left_text)
    TextView tvToRightLeftText;


    public NavBar(Context context) {
        super(context);
    }

    public NavBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NavBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_navi;
    }

    @Override
    protected void initView() {
//        ButterKnife.bind(this, this);

//        setImageBackground(R.mipmap.x);
//        setColorRes(R.color.colorPrimary);
//        setNavColorRes(R.color.colorPrimary);
        setStatusBarColorRes(R.color.colorPrimary);
    }

    @OnClick(R.id.ll_back)
    public void onClick() {
        ((Activity) getContext()).finish();
    }

    public void showLeftText(String str, View.OnClickListener listener) {
        if (!TextUtils.isEmpty(str)) {
            tvLeftText.setText(str);
        }
        tvLeftText.setOnClickListener(listener);
    }

    public TextView getTitleTextView() {
        return tvTitle;
    }

    public void setTitle(String str) {
        tvTitle.setText(str);
    }

    public void setTitle(int str) {
        tvTitle.setText(str);
    }

    public void setBackClickListener(View.OnClickListener backClickListener){
        ivBack.setOnClickListener(backClickListener);
    }

    public void hideBack() {
        ivBack.setVisibility(GONE);
    }

    public void showBack() {
        ivBack.setVisibility(VISIBLE);
    }

    public void setBackIcon(int res){
        ivBack.setBackgroundResource(res);
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) ivBack.getLayoutParams();
        params.width= LinearLayout.LayoutParams.WRAP_CONTENT;
        params.height= LinearLayout.LayoutParams.WRAP_CONTENT;
        params.leftMargin= DisplayUtil.dip2px(getContext(),10);
        ivBack.setLayoutParams(params);
    }


    public void setLeftTextAndImage(int resourceId, String str, View.OnClickListener listener) {
        tvLeftText.setVisibility(VISIBLE);
        showLeftText(TextUtils.isEmpty(str) ? "" : str, listener);
        Drawable drawable = getResources().getDrawable(resourceId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvLeftText.setCompoundDrawables(drawable, null, null, null);
    }

    public void setRightText(String str, View.OnClickListener listener) {
        tvRightText.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(str)) {
            tvRightText.setText(str);
        }
        if (listener!=null) {
            tvRightText.setOnClickListener(listener);
        }
    }

    public void setRightText(String str) {
        tvRightText.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(str)) {
            tvRightText.setText(str);
        }
    }

    public void setLeftTextOfRightText(String str,View.OnClickListener listener){
        tvToRightLeftText.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(str)) {
            tvToRightLeftText.setText(str);
        }
        if (listener!=null) {
            tvToRightLeftText.setOnClickListener(listener);
        }
    }

    public void setLeftOfRightTextAndImage(int resourceId, String str, View.OnClickListener listener){
        tvToRightLeftText.setVisibility(VISIBLE);
        setLeftTextOfRightText(TextUtils.isEmpty(str) ? "" : str, listener);
        Drawable drawable = getResources().getDrawable(resourceId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvToRightLeftText.setCompoundDrawables(drawable, null, null, null);
    }

    public void setRightTextAndImage(int resourceId, String str, View.OnClickListener listener) {
        tvRightText.setVisibility(VISIBLE);
        setRightText(TextUtils.isEmpty(str) ? "" : str, listener);
        if (resourceId!=0) {
            Drawable drawable = getResources().getDrawable(resourceId);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvRightText.setCompoundDrawables(drawable, null, null, null);
        }
    }

    public void setRightIcon(int resourceId){
        tvRightText.setVisibility(VISIBLE);
        Drawable drawable = getResources().getDrawable(resourceId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvRightText.setCompoundDrawables(drawable, null, null, null);
    }

    public void setRightIcon(int resourceId,View.OnClickListener clickListener){
        tvRightText.setVisibility(VISIBLE);
        Drawable drawable = getResources().getDrawable(resourceId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tvRightText.setCompoundDrawables(drawable, null, null, null);
        tvRightText.setOnClickListener(clickListener);
    }

}
